<?php defined('SYSPATH') or die('No direct script access.');
  
class Controller_Cms_Users extends Controller_Cms_Setbackend
{
	private $model;
	
	/*public function before()
	{
		parent::before();
    }*/
    
	public function action_edituser($id_user)
	{
		if($id_user == 'my')
			$id_user = Auth::instance()->get_user()->id;

		if($id_user == 'new')
		{
			$user = ORM::factory('user');
			$this->template->page_title = "Administracja :: Nowy użytkownik";
			$this->template->breadcrumb = html::anchor('admin-panel','Pulpit')." > ".html::anchor('admin/users/list','Lista użytkowników')." > Nowy użytkownik";
		}else{
			$user = ORM::factory('user', $id_user);
			$profil = ORM::factory('user',$id_user)->profiles;
			$this->template->page_title = "Administracja :: Edycja użytkownika";
			$this->template->breadcrumb = html::anchor('admin','Start')." > Użytkownik: ".$profil->imie . " " . $profil->nazwisko;
		}
		$view = $this->template->content = View::factory('cms/backend_user_register');
		
		if($_POST)
		{
			//echo Kohana::debug($_POST);exit;
			//$post = $user->validate_change($_POST);
			//if ($post->check())
			//{
				$sanit_email = Security::xss_clean($_POST['email']);
				$user->email = $sanit_email;
				$user->veryfied = 'true';
				if($id_user == 'new')
					$user->created_at = time();
				if(!empty($_POST['password'])) $user->password = Security::xss_clean($_POST['password']);
				$user->save();
				$last = mysql_insert_id();
				if($id_user == 'new')
				{
					$profil = ORM::factory('profile');
					$profil->user_id = $last;
				}
				$sanit_imie = Security::xss_clean($_POST['imie']);
				$sanit_nazwisko = Security::xss_clean($_POST['nazwisko']);
				$profil->imie = $sanit_imie;
				$profil->nazwisko = $sanit_nazwisko;
				$profil->save();
				if($id_user == 'new')
					$red = $last;
					else $red = $id_user;
				Session::instance()->set('flash_panel','<div class="flash-ok">Dane zostały zmienione</div>');
				Request::instance()->redirect('admin/edituser/'.$red);
			/*}
			 else
			{
				Session::instance()->set('flash_panel','<div class="flash-error">'.i18n::get('invalid_data').'</div>');
				$view->post = $post->as_array();
				$view->errors = $post->errors('validate');
				$view->edit = true;
			} */
			Request::instance()->redirect('admin/edituser/'.$id_user);
		}
		$view->id_user = $id_user;
		if($id_user != 'new')
		{
			$view->data = $user;
			$view->check_edit = $id_user;
		}else{
			$view->check_edit = 'false';
		}
		$view->user = $id_user;
		$this->template->flag_switch = 'without';
		$this->template->content = $view;
	}
	
	public function action_checkmail()
	{
		if (Request::$is_ajax)
		{
			$edit = $_POST['edit'];
			$this->auto_render = false;
			if($edit == 'false')
			{
				$exists = (bool) DB::select(array('COUNT("*")', 'total_count'))
					->from('users')
					->where('email','=',trim($_POST['email']))
					->execute()
					->get('total_count');
			}else{
				$exists = (bool) DB::select(array('COUNT("*")', 'total_count'))
					->from('users')
					//->where('id','!=',Auth::instance()->get_user()->id)
					->where('id','!=',$edit)
					->and_where('email','=',trim($_POST['email']))
					->execute()
					->get('total_count');
			}
			echo $exists;
		}
	}
}
